package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ui.*;

// a common superclass for different kind of mouse action to game command
// i.e. move straight ahead
// rotate
// side step, etc.
public class MouseStraightForwardHandler extends MouseStraightHandler {
  // Shape origSelectedRectangle;


  public MouseStraightForwardHandler() {
    super();
    id = H_STRAIGHTFORWARD;
	unitSelectedText = "Drag mouse forward and release button to move unit.";
	noUnitSelectedText = "Click on a unit to select for forward movement.";
  }


  /**
   * newP already scaled
   */
  protected void dragSelected( Point newP ) {
    // attempt to use transform on point to see if we can change
    // coordinate to conform to that of the new user space,
    // then just translate from that
    //
	selectedTrans = ((SelectedElementContainer)selectedElements.get(0)).element.getTransform();

	// Point scaledEndPoint = ScalingDelegate.getMouseEventLocation(newP);
	
    try {
      endDragP = selectedTrans.inverseTransform(newP, null);
    } catch ( Exception ex ) {
      ex.printStackTrace();
    }
    // now figure out just the Y difference between the 2 points
    double diffY = endDragP.getY() - startDragP.getY();
    
     
    // for forward movement only
    if ( diffY < 0 ) {
		AffineTransform currTrans;
		Shape originalRec;
	
		for (int i=0; i < selectedElements.size(); i++ ){
			SelectedElementContainer selEl = (SelectedElementContainer)selectedElements.get(i);
			Element el = selEl.element;
			AffineTransform trans= el.getTransform();
			currTrans = (AffineTransform)trans.clone();
			currTrans.translate(0, diffY);
			originalRec = el.getRectangle();
			selEl.selectedRectangle = currTrans.createTransformedShape(originalRec);
			selEl.currTrans = currTrans;
		
//			Rectangle2D rec2update = selEl.selectedRectangle.getBounds2D();
//			updateSelectedAreaForRepaint(rec2update);
			
			DirtyAreaManager.addDirtyArea(selEl.selectedRectangle);
		
		}
		pixMoved = -diffY;

    } else {
    }
    return;
  }
}